var app = getApp();
Page({
	data: {
		order: {},
		id: null
	},
	onLoad(options) {
		var $th = this;
		if (options) {
			if (options.id) {
				$th.data.id = options.id
			}
		}
		app.globalAjax(
			'/api/order/detail',
			"get", {
				id: $th.data.id
			},
			function(e) {
				if (e.code == 0) {
					$th.setData({
						order: e.order
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			})
	},
	againorder: function() { //再来一单
		var that = this,
			$data = that.data
		var total = $data.order.totalAmount
		total = parseFloat(total).toFixed(2)
		var orderdata = {
			orderGoodsList: $data.order.orderGoodsList,
			total: total, //合计
			remark: "", //备注
			storeId: 0, //供应商ID
			totalAmount: 0, //总金额
		}
		wx.setStorage({
			key: "orderlist",
			data: orderdata //需要存储的数据
		})
		wx.redirectTo({
			url: '/pages/cart/pay/pay'
		})
	},
	refundOrder: function(e){
		var orderId = this.data.id;
		wx.navigateTo({
			url:'/pages/order/refund/refund?id=' + orderId
		})
	},
	cancleOrder: function(e){
		var id = this.data.id;
		app.globalAjax(
			'/api/order/cancel',
			"get",
			{id:id},
			function(e) {
				if (e.code == 0) {
					wx.showToast({
						title: '取消成功',
						icon: "none"
					});
					wx.navigateBack()
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})
	},
	Qpay: function(e) { //订单付款
		var that = this
		app.globalAjax(
			'/api/unifiedOrder',
			"post", {
				orderNumber: that.data.order.orderNumber,
				totalAmount: 0.1
			},
			function(e) {
				if (e.code == 0) {
					if(!e.data){
						wx.showToast({
							title: '支付成功'
						})
						that.onLoad()
						return
					}
					app.pays(e.data, function(ee) {
						that.onLoad()
					}, function() {
						console.log("付款失败")
					})
				} else {
					wx.showToast({
						title: e.msg,
						icon: "none"
					});
				}
			}, {
				token: wx.getStorageSync("token")
			})
	}
})
